package com.example.a16478082;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MenuListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menulist);

        //绑定XML中的ListView，作为Item的容器
        ListView list = (ListView) findViewById(R.id.MyListView);

        //生成动态数组，并且转载数据
        ArrayList<HashMap<String,Object>> mylist = new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<30;i++){
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("photo",R.mipmap.xcr);
            map.put("ItemTitle", "小炒肉.");
            map.put("ItemPrice", "30RMB");
            mylist.add(map);
        }

        //生成适配器，数组===》ListItem
        SimpleAdapter mSchedule = new SimpleAdapter(
                this, //没什么解释
                mylist, //数据来源
                R.layout.my_listitem,//ListItem的XML实现
                //动态数组与ListItem对应的子项
                new String[] {"photo", "ItemTitle","ItemPrice"},

                //ListItem的XML文件里面的两个TextView ID
                new int[] {R.id.photo,R.id.ItemTitle,R.id.ItemPrice});

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ImageView phone=view.findViewById(R.id.photo);
                TextView ItemTitle=view.findViewById(R.id.ItemTitle);
                TextView ItemPrice=view.findViewById(R.id.ItemPrice);
                Toast.makeText(MenuListActivity.this,"此菜品已选择",Toast.LENGTH_SHORT).show();
            }
        });
        //添加并且显示
        list.setAdapter(mSchedule);
    }
}

